
discuss_obs %>%
  count_prop(discuss_type, group_obs_trans_included, who_spoke_pro_trans) %>%
  select(phase, group_obs_trans_included, discuss_type, who_spoke_pro_trans) %>%
  filter(discuss_type == "discussion_full", group_obs_trans_included==TRUE) %>%
  mutate(n_spoke_pro_trans = str_split(who_spoke_pro_trans, " ") %>%
    map_dbl(~ sum(.x %in% c("1", "2", "3")))
  ) %>%

  ggplot(aes(x = n_spoke_pro_trans)) +
  geom_histogram(binwidth = 1, fill = "indianred", colour = "white")


discuss_obs %>%
  count_prop(pos_mentions, neg_mentions)

discuss_obs %>% count_prop(neg_mentions, pos_mentions)

discuss_obs %>%
  select(group_id, round, who_spoke_pro_trans) %>%
  left_join(df %>% select(group_id, ind_id)) %>%
  mutate(ind_id_no_group = ind_id %>% str_replace(group_id, "") %>% str_replace("_", "")) %>%
  mutate(ind_spoke_pro_trans = str_detect(who_spoke_pro_trans, ind_id_no_group)) %>%
  group_by(group_id, ind_id) %>%
  summarise(p_spoke_pro_trans = mean_na(ind_spoke_pro_trans))


models_discussion_dominance <- list(
  "Combined index (Z)" = feols_custom(
    r2_choose_trans ~ overall_dominance + overall_dominance_trans + item_diff + r2_reliability_diff + r2_reliability_shown,
    data = r2_with_discuss_obs %>% filter(discuss_type == "discussion_full"),
    fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order_in_pair", "phase"),
    cluster = "group_id"
  ),
  "Spoke first" = feols_custom(
    r2_choose_trans ~ p_spoke_first + p_spoke_first_trans + item_diff + r2_reliability_diff + r2_reliability_shown,
    data = r2_with_discuss_obs %>% filter(discuss_type == "discussion_full"),
    fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order_in_pair", "phase"),
    cluster = "group_id"
  ),
  "Was dominant" = feols_custom(
    r2_choose_trans ~ p_dominant + p_dominant_trans + item_diff + r2_reliability_diff + r2_reliability_shown,
    data = r2_with_discuss_obs %>% filter(discuss_type == "discussion_full"),
    fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order_in_pair", "phase"),
    cluster = "group_id"
  )
)

tex_export(
  models_discussion_dominance,
  file = "outputs/tables/discussion_dominance.tex",
  coef_rename = coef_label,
  gof_map = fe_label_no_fe,
  coef_omit = vars_to_regex(c("stratum_id", "Intercept", "video", "group_control", "pair_includes_trans_alt$", "phase", control_vars, "delivery")),
  additional_header = vec_to_custom_header(c(" ", rep("Dep var: Chose trans in private outcome round (=1)", 3)))
)


coeff_spoke_first_trans <- (models_discussion_dominance$`Spoke first` %>% get_coeff("p_spoke_first_trans"))
mean_spoke_first <- r2_with_discuss_obs %>% filter(discuss_type == "discussion_full") %>%
  pull(p_spoke_first) %>% mean_na()
perc_spoke_first <- coeff_spoke_first_trans / mean_spoke_first

coeff_spoke_first_trans %>% times_100 %>% write_stat("outputs/stats/coeff_spoke_first_trans.tex", digits = 0)
perc_spoke_first %>% write_percentage("outputs/stats/perc_spoke_first.tex")

coeff_dominant <- (models_discussion_dominance$`Was dominant` %>% get_coeff("p_dominant_trans"))
mean_dominant <- r2_with_discuss_obs %>% filter(discuss_type == "discussion_full") %>%
  pull(p_dominant) %>% mean_na()
  

mean_dominant %>% write_stat("outputs/stats/mean_dominant.tex", digits = 2)
perc_dominant <- coeff_dominant / mean_dominant

coeff_dominant %>% times_100 %>% write_stat("outputs/stats/coeff_dominant.tex", digits = 0)
perc_dominant %>% write_percentage("outputs/stats/perc_dominant.tex")


r2_with_discuss_obs$p_dominant

models_discussion_dominance$`Spoke first` %>% get_p_val("p_spoke_first_trans") %>% write_stat("outputs/stats/spoke_first_p_val.tex", digits = 2, p_value = TRUE)
models_discussion_dominance$`Was dominant` %>% get_p_val("p_dominant_trans") %>% write_stat("outputs/stats/dominant_p_val.tex", digits = 2, p_value = TRUE)


# Correlation between dominance index for trans and for non-trans
# Calculate correlation between dominance index for trans vs non-trans choices
cor_dominance <- r2_with_discuss_obs %>%
  filter(discuss_type == "discussion_full") %>%
  select(ind_id, overall_dominance, overall_dominance_trans, overall_dominance_nontrans) %>%
  distinct() %>%
  drop_na() %>%
  summarise(
    correlation = cor(overall_dominance_trans, overall_dominance_nontrans, use = "complete.obs")
  ) %>%
  pull(correlation)


# Export correlation coefficient
cor_dominance %>% write_stat("outputs/stats/cor_dominance_trans_nontrans.tex", digits = 2)

# Calculate p-value for correlation
cor_test <- r2_with_discuss_obs %>%
  filter(discuss_type == "discussion_full") %>%
  with(cor.test(overall_dominance_trans, overall_dominance))

# Export p-value
cor_test$p.value %>% write_p_val("outputs/stats/cor_dominance_pval.tex")


# What demographics predict speaking up? ----------------------------------------------------------------------------------------------------------------

list(
  feols_custom(
    as.formula(str_glue("overall_dominance_trans ~ {fml_sum(demo_vars)} + item_diff + r2_reliability_diff + r2_reliability_shown")),
    data = r2_with_discuss_obs %>% filter(discuss_type == "discussion_full"),
    fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order_in_pair", "phase"),
    cluster = "group_id"
  ),
  feols_custom(
    as.formula(str_glue("overall_dominance_trans ~ overall_dominance + {fml_sum(demo_vars)} + item_diff + r2_reliability_diff + r2_reliability_shown")),
    data = r2_with_discuss_obs %>% filter(discuss_type == "discussion_full"),
    fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order_in_pair", "phase"),
    cluster = "group_id"
  ),
  feols_custom(
    as.formula(str_glue("overall_dominance_trans ~ overall_dominance + {fml_sum(demo_vars)} + item_diff + r2_reliability_diff + r2_reliability_shown")),
    data = r2_with_discuss_obs %>% filter(discuss_type == "discussion_full"),
    fixef = c("group_id", "stratum_id", "video_type", "delivery_incentive_exp", "comparator_order_in_pair", "phase"),
    cluster = "group_id"
  )
) %>%
  tex_export()